package org.microser.gataway.zuul.config;

import com.netflix.discovery.converters.Auto;
import lombok.Data;
import org.microser.gataway.zuul.filter.DefinedZuulFilter;
import org.microser.gataway.zuul.filter.JwtFilter;
import org.microser.gataway.zuul.security.entity.PermissionPath;
import org.microser.gataway.zuul.security.service.PermissionPathService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 配置过滤器和过滤地址
 *
 * @author liujiamiao
 * @email 2544296058@qq.com
 * @date 2020-11-24 9:04
 */
public class SecurityFilter {

    @Autowired
    private DefinedZuulFilter jwtFilter;

    @Autowired
    private PermissionPathService permissionPathService;

    private List<PermissionPath> uriFilter = null;

    private List<PermissionPath> uriPermission = null;

    /**
     * 配置什么路由给什么过滤器过滤
     * @return
     */
    public LinkedHashMap<String,String> getUri(){
        LinkedHashMap<String,String> map = new LinkedHashMap();
        if(uriFilter==null){
            uriFilter = permissionPathService.getFilterPermission();
        }
        uriFilter.stream().forEach(entiy->{
            map.put(entiy.getPath(),entiy.getFilter());
        });

//        map.put("/api/doc.html","anon");
//        map.put("/api/auth/login","anon");
//        map.put("/api/common/crypto/getPublicKey","anon");
//        map.put("/api/common/captcha/code","anon");
//        map.put("/api/**","jwtToken");
//        map.put("/user/**","anon");

        return map;
    }

    /**
     * 配置first下的过滤器
     * @return
     */
    public LinkedHashMap<String, DefinedZuulFilter> getFilter(){
        LinkedHashMap<String,DefinedZuulFilter> map = new LinkedHashMap();
        map.put("anon",null);
        map.put("jwtToken",jwtFilter);
        return map;
    }

    /**
     * 配置什么路径需要什么权限
     * 角色和权限可以同时使用
     *   , =>是并且
     *   ||=>是或者
     * @return
     */
    public LinkedHashMap<String, String> securityPath(){
        //注意：加uri权限的同时，该uri必须有过滤器，如果过滤器都没有，就不存在权限了
        LinkedHashMap<String,String> map = new LinkedHashMap();
        if(uriPermission==null){
            uriPermission = permissionPathService.getURIPermission();
        }
        uriPermission.stream().forEach(entity->{
            map.put(entity.getPath(),entity.getPermission());
        });

//        map.put("/api/admin/list","user:list||user");//需要 user:list or user
//        map.put("/api/admin/add","user:add");//需要 user:add
//        map.put("/api/admin/delete","user,user:delete");//需要 user and user:delete
//        map.put("/api/admin/**","admin,roles[a,b,c],user:add");//需要 admin and 角色(a and b and c) and user:add
//        map.put("/api/abc/**","roles[admin||user],user:delete");//需要 角色(admin or user) and user:delete
//        map.put("/api/abd/**","roles[admin||user]||user:delete");//需要 角色(admin or user) or user:delete
        return map;
    }

}
